package com.fas.system.service.impl;

import com.fas.common.core.domain.AjaxResult;
import com.fas.common.utils.StringUtils;
import com.fas.system.domain.FasCooperationCompany;
import com.fas.system.mapper.FasBuildingMapper;
import com.fas.system.mapper.FasCooperationCompanyMapper;
import com.fas.system.service.IFasCooperationCompanyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class FasCooperationCompanyServiceImpl implements IFasCooperationCompanyService {

    @Resource
    private FasCooperationCompanyMapper fasCooperationCompanyMapper;
    @Resource
    private FasBuildingMapper fasBuildingMapper;

    public static Map<Integer,String> COMPANY_NAMES = new HashMap<>();

    /**
     * 项目启动时，初始化数据到缓存
     */
    @PostConstruct
    public void init() {
        loadingDictCache();
    }

    @Override
    public List<FasCooperationCompany> queryAll(FasCooperationCompany fasNetworkingCompany) {
        return fasCooperationCompanyMapper.queryAll(fasNetworkingCompany);
    }

    @Transactional
    @Override
    public AjaxResult insertFasCooperationCompany(FasCooperationCompany fasNetworkingCompany) {
        int exists = fasCooperationCompanyMapper.existsCompanyName(fasNetworkingCompany.getCompanyName());
        if (exists > 0) {
            return AjaxResult.error("该公司名称已存在！");
        }
        int count = fasCooperationCompanyMapper.insertFasCooperationCompany(fasNetworkingCompany);
        return saveToRedis(fasNetworkingCompany, count);
    }

    @Transactional
    @Override
    public AjaxResult updateFasCooperationCompany(FasCooperationCompany fasNetworkingCompany) {
        int exists = fasCooperationCompanyMapper.existsCompanyNameById(fasNetworkingCompany);
        if (exists > 0) {
            return AjaxResult.error("该公司名称已存在！");
        }
        int count = fasCooperationCompanyMapper.updateFasCooperationCompany(fasNetworkingCompany);
        return saveToRedis(fasNetworkingCompany, count);
    }

    /**
     * 将合作单位数据保存至redis
     *
     * @param fasNetworkingCompany
     * @param count
     * @return
     */
    public AjaxResult saveToRedis(FasCooperationCompany fasNetworkingCompany, int count) {
        if (count > 0) {
            Integer id = fasNetworkingCompany.getId();
            if (id == null) {
                throw new RuntimeException("合作单位id为空！");
            }
            COMPANY_NAMES.put(id,fasNetworkingCompany.getCompanyName());
        }
        return count > 0 ? AjaxResult.success() : AjaxResult.error();
    }


    @Transactional
    @Override
    public AjaxResult deleteFasCooperationCompany(Integer[] ids) {
        List<Integer> errorIds = new ArrayList<>();
        List<Integer> deleteDataList = new ArrayList<>();
        if (ids.length > 0) {
            for (Integer id : ids) {
                int exists = fasBuildingMapper.existsByCooperationCompanyId(id);
                if (exists > 0)
                    errorIds.add(id);
                else {
                    deleteDataList.add(id);
                    COMPANY_NAMES.remove(id);
                }
            }
        }
        if (errorIds.size() > 0) {
            return AjaxResult.error("这些合作单位下有建筑不能删除，合作单位编号为："+StringUtils.join(errorIds, ","));
        }
        int count = 0;
        if (deleteDataList.size() > 0) {
            count = fasCooperationCompanyMapper.deleteFasCooperationCompany(deleteDataList);
        }
        return count > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 缓存单位id和单位名称至redis数据库
     */
    private void loadingDictCache() {
        List<FasCooperationCompany> fasCooperationCompanies = fasCooperationCompanyMapper.queryAll(new FasCooperationCompany());
        COMPANY_NAMES = fasCooperationCompanies.stream().collect(Collectors.toMap(FasCooperationCompany::getId, FasCooperationCompany::getCompanyName, (v1, v2) -> v1));
    }

}
